import { GlobVar } from "../global";

import compress from "koa-compress";
import mount from "koa-mount";
import render from "koa-ejs";
import serve from "koa-static";
const range = require("koa-range");
const session = require("koa-session-minimal");
const MysqlStore = require("koa-mysql-session");

import { getServerConfig, root } from "../libs/utils";
import { Context } from "koa";

const serverConfig = getServerConfig().server;

const boot = (
  app: import("koa")<import("koa").DefaultState, import("koa").DefaultContext>
): void => {
  app.use(
    session({
      // store: undefined,
      store:
        !GlobVar.MODE_DEV && serverConfig.mysqlExist
          ? new MysqlStore(serverConfig.mysql)
          : undefined,
      key: serverConfig.sessionKey,
      maxAge: serverConfig.sessionTimeout,
      signed: true,
    })
  );

  // compress middleware
  app.use(
    compress({
      // filter(contentType: string) {
      //   return /(text|javascript)/i.test(contentType)
      // },
      threshold: 2048,
      br: false,
      gzip: {
        flush: require("zlib").constants.Z_SYNC_FLUSH,
      },
      // flush: require('zlib').Z_SYNC_FLUSH,
    })
  );

  // serve static files
  const httpStatic = serverConfig.http.static[process.env.NODE_ENV];
  app.use(range); // 视频文件进度条无法拖拽问题
  for (const [k, v] of Object.entries(httpStatic)) {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore

    app.use(mount(k, serve(root(v), { index: false, maxage: 604800000 })));
  }
  if (GlobVar.MODE_DEV) {
    app.use(async (ctx: Context, next: any) => {
      if (
        /(\.hot-update\.)|(\.(png|jpg|ttf|otf|eot|woff2?)(\?.+)?$)|(\.js$)/.test(
          ctx.url
        )
      ) {
        ctx.redirect(`http://localhost:8001${ctx.url}`);
      } else {
        await next();
      }
    });
  }

  render(app, {
    root: root("server/views"),
    cache: !GlobVar.MODE_DEV,
    layout: false,
  });
};

export default boot;
